package com.dtda.edu.miaosha.service.impl;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.thymeleaf.util.StringUtils;

import com.dtda.edu.miaosha.bo.UserInfoBo;
import com.dtda.edu.miaosha.dao.UserInfoPoMapper;
import com.dtda.edu.miaosha.dao.UserPasswordPoMapper;
import com.dtda.edu.miaosha.po.UserInfoPo;
import com.dtda.edu.miaosha.po.UserPasswordPo;
import com.dtda.edu.miaosha.po.UserPasswordPoExample;
import com.dtda.edu.miaosha.service.UserService;

/**
 * @author 袁强
 * @version 2019年1月14日 下午9:18:33
 * 类说明  
 */
@Service("userService")
public class UserServiceImpl implements UserService{

	@Autowired
	private UserInfoPoMapper userInfoPoMapper;
	@Autowired
	private UserPasswordPoMapper userPasswordPoMapper;
	public UserInfoBo getUserById(Integer id) {
		// TODO Auto-generated method stub
		UserInfoPo userInfoPo = userInfoPoMapper.selectByPrimaryKey(id);
		if(userInfoPo == null) {
			return null;
		}
		UserPasswordPoExample userPasswordPoExample = new UserPasswordPoExample();
		userPasswordPoExample.createCriteria().andUserIdEqualTo(userInfoPo.getId());
		List<UserPasswordPo> userPasswordPo = userPasswordPoMapper.selectByExample(userPasswordPoExample);
		if(userPasswordPo == null ) {
			return null;
		}
		UserInfoBo userInfoBo = new UserInfoBo();
		BeanUtils.copyProperties(userInfoPo, userInfoBo);
		userInfoBo.setEncrptPassword(userPasswordPo.get(0).getEncrptPassword());
		return userInfoBo;
	}
	public UserInfoBo selectByTel(String telphone) {
		// TODO Auto-generated method stub
		UserInfoPo userInfoPo = userInfoPoMapper.selectByTel(telphone);
		if(userInfoPo == null) {
			return null;
		}
		UserInfoBo userInfoBo = new UserInfoBo();
		BeanUtils.copyProperties(userInfoPo, userInfoBo);
		return userInfoBo;
	}
	public int insertUserInfo(UserInfoBo userInfoBo) {
		// TODO Auto-generated method stub
		if(userInfoBo == null) {
			return 0;
		}
		UserInfoPo userInfoPo = new UserInfoPo();
		BeanUtils.copyProperties(userInfoBo, userInfoPo);
		userInfoPo.setRegisterMode("byphone");
		int i = userInfoPoMapper.insertUserInfo(userInfoPo);
		if(i <=0) {
			return 0;   //添加用户信息失败
		}
		UserPasswordPo userPasswordPo = new UserPasswordPo();
		userPasswordPo.setEncrptPassword(userInfoBo.getEncrptPassword());
		UserInfoPo userInfoPo2 = userInfoPoMapper.selectByTel(userInfoPo.getTelphone());
		if(userInfoPo2 != null) {
			userPasswordPo.setUserId(userInfoPo2.getId());
			int j = userPasswordPoMapper.insert(userPasswordPo);
			if(j <=0) {
				return 0;  //添加密码失败,即整个插入失败
			}
			i=j;
		}
		return i;
	}
	public UserInfoBo selectByTelandPass(String telphone, String encrptPassword) {
		// TODO Auto-generated method stub
		
		UserInfoPo userInfoPo = userInfoPoMapper.selectByTel(telphone);
		if(userInfoPo == null) {
			return null;
		}
		UserInfoBo userInfoBo = new UserInfoBo();
		BeanUtils.copyProperties(userInfoPo, userInfoBo);
		
		List<UserPasswordPo> passwordPos = userPasswordPoMapper.selectByPass(encrptPassword);
		/*if(passwordPos.size() <=0) {
			return null;
		}*/
		Boolean equals = false;
		if(passwordPos.size() >0) {
			e :for (UserPasswordPo userPasswordPo : passwordPos) {
				equals = StringUtils.equals(userInfoPo.getId(), userPasswordPo.getUserId());
				if(equals) {
					userInfoBo.setEncrptPassword(userPasswordPo.getEncrptPassword());
					break e;
				}
			}
		}else {
			return null;
		}
		
		if(! equals) {
			//密码和对应注册的手机号不匹配
			return null;
		}
		
		return userInfoBo;
	}

}
